<script setup>
import { getPayInfoAPI, payOrderAPI,cancelOrderAPI } from '@/apis/checkout';
import { ref, onMounted,watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useCountDown } from '@/composables/useCountDown'
const route = useRoute() // 路由信息
const router = useRouter() // 路由实例
const payInfo = ref({}) // 支付信息对象
const payType = ref(0);
const ispay = ref(false);
const dialogVisible = ref(false)
// 倒计时
const { formatTime, start } = useCountDown()
// 调用接口 传入id 获取支付信息
const getOrder = async () => {
  await getPayInfoAPI(route.params.id).then((res) => {
    payInfo.value = res.data
    // 倒计时15分钟
    start(15 * 60)
  })
}
// 监听时间
watch(formatTime, (newValue) => {
  if (newValue === '00分00秒') {
    // 取消订单
    cancelOrderAPI(payInfo.value.orderNo).then((res) => {
      ElMessage.warning("订单超时已取消")
      router.push('/')
    })
  }
})
onMounted(() => getOrder())

// 处理支付
const handlePay = async () => {
  ispay.value = true
  await payOrderAPI({
    orderNo: payInfo.value.orderNo,
    payType: payType.value,
    isPay: ispay.value
  }).then((res) => {
    dialogVisible.value = false
    ElMessage.success(res.message)
    router.push('/order')
  }).catch((err) => {
    console.log(err);
    ElMessage.error(res.message)
  })
}

// 处理未支付
const handleNoPay = async () => {
  await payOrderAPI({
    orderNo: payInfo.value.orderNo,
    payType: payType.value,
    isPay: ispay.value
  }).then((res) => {
    ElMessage.warning(res.message)
    dialogVisible.value = false
  }).catch((err) => {
    console.log(err);
  })
}

const payOrder = (type) => {
  dialogVisible.value = true
  payType.value = type
}

</script>



<template>
  <div class="pay-page">
    <div class="container">
      <!-- 付款信息 -->
      <div class="pay-info">
        <span class="icon iconfont icon-queren2"></span>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付。</p>
          <p>支付还剩 <span>{{ formatTime }}</span>, 超时后将取消订单</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>¥{{ payInfo.payMoney?.toFixed(2) }}</span>
        </div>

      </div>
      <hr>
      <div class="info">
        <el-row>
          <el-col :span="6">订单号：</el-col>
          <el-col :span="18">
            <p>{{ payInfo.orderNo }}</p>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="6">收货地址：</el-col>
          <el-col :span="18">
            <p>{{ payInfo.receiveName }}</p>
            <p>{{ payInfo.receivePhone }}</p>
            <p>{{ payInfo.address }}</p>
          </el-col>
        </el-row>
        <el-row v-for="(item, index) in payInfo.products" :key="index">
      <el-col :span="6">商品{{index}}：</el-col>
      <el-col :span="18">
        <img v-if="item.productImage" :src="'/api/images/' + item.productImage" alt="">
        <p>{{ item.productName }}</p>
      </el-col>
    </el-row>

      </div>

      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;" @click="payOrder(1)"></a>
          <a class="btn alipay" href="javascript:;" @click="payOrder(2)"></a>
        </div>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="支付确认" width="30%">
      <span>您是否完成订单?</span>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handlePay">已支付</el-button>
          <el-button @click="handleNoPay">
            未支付
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>

</template>

<style scoped lang="scss">
.pay-page {
  margin-top: 20px;
}

.pay-info {

  background: #fff;
  display: flex;
  align-items: center;
  height: 240px;
  padding: 0 80px;

  .icon {
    font-size: 80px;
    color: #1dc779;
  }

  .tip {
    padding-left: 10px;
    flex: 1;

    p {
      &:first-child {
        font-size: 20px;
        margin-bottom: 5px;
      }

      &:last-child {
        color: #999;
        font-size: 16px;
      }
    }
  }

  .amount {
    span {
      &:first-child {
        font-size: 16px;
        color: #999;
      }

      &:last-child {
        color: $priceColor;
        font-size: 20px;
      }
    }
  }

}

.info {
  padding: 20px 80px;

  .el-row {
    padding: 10px 0;
  }

  img {
    width: 20px;
    height: 20px;
    margin-right: 10px; // 添加间距
  }

  p {
    display: inline-block;
    color: #999;
    font-size: 14px;
    text-align: right; // 使 p 元素在右侧对齐
  }
}

.pay-type {
  margin-top: 20px;
  background-color: #fff;
  padding-bottom: 70px;

  p {
    line-height: 70px;
    height: 70px;
    padding-left: 30px;
    font-size: 16px;

    &.head {
      border-bottom: 1px solid #f5f5f5;
    }
  }

  .btn {
    width: 150px;
    height: 50px;
    border: 1px solid #e4e4e4;
    text-align: center;
    line-height: 48px;
    margin-left: 30px;
    color: #666666;
    display: inline-block;

    &.active,
    &:hover {
      border-color: $xtxColor;
    }

    &.alipay {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
    }

    &.wx {
      background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
    }
  }
}
</style>